<template>
  <BasicModal v-bind="$attrs" @register="register" showFooter :title="getTitle" width="70%" @ok="handleSubmit"
    :loading="loading">
    <!-- 审批进度 -->
    <div style="width: 100%; height: 100%">
      <!-- v-on="gridEvents" overflow: hidden -->
      <vxe-grid ref="tableRef" v-bind="gridOptions">
        <template #toolbar_buttons>

        </template>
        <template #footer_totalAllPrice>
          <span style="color: red;">{{ totalObj.totalAllPrice }}{{ totalObj.totalAllPrice ? '元' : '' }}</span>
        </template>
        <template #footer_orderCommissionPrice>
          <span style="color: red;">{{ totalObj.orderCommissionPrice }}{{ totalObj.orderCommissionPrice ? '元' : ''
            }}</span>
        </template>

        <template #action="{ row }">
          <TableAction outside :actions="createActions(row)" />
        </template>
      </vxe-grid>
    </div>

  </BasicModal>
</template>
<script lang="ts" setup>
import { ref, computed, unref, h, toRaw, reactive } from 'vue';

import {
  BasicTableProps,
  VxeGridInstance
} from '@/components/VxeTable';
import {
  addExcludeColumns
} from './role.data';

import { checkboxCountFn } from './role.data';
import { BasicModal, useModalInner } from '@/components/Modal';

import { BasicTree, TreeItem } from '@/components/Tree';
import { getExcludeAffirmList, removeExcludeAffirmLog, addExcludeZhongBuLog } from '@/api/heng/ywManage';
import { BasicTable, useTable, TableAction, ActionItem } from '@/components/Table';

import { Modal, Timeline, TimelineItem } from 'ant-design-vue';

import { useMessage } from "@/hooks/web/useMessage";
import util from '@/utils/util';
import { t } from '@/hooks/web/useI18n';

const emit = defineEmits(['success', 'register']);
const isUpdate = ref(true);
const treeData = ref<TreeItem[]>([]);
const { createMessage } = useMessage();
const mouldMainId = ref("");

const applyViewShow = ref(false);
const visible = ref(false);
const msgList = ref([]);

const tableRef = ref<VxeGridInstance>();
const num = ref(0);
const gridOptions = reactive<BasicTableProps>({
  // keepSource: true,
  border: true,
  showOverflow: true,
  size: 'mini',
  height: 'auto', // 'auto'
  columns: addExcludeColumns,
  filterConfig: {
    remote: true,
  },
  exportConfig: {},
  showFooter: true,
  footerData: [
    { orderNo: '合计', type: "" }
  ],
  toolbarConfig: {
    slots: {
      buttons: 'toolbar_buttons',
    },
    refresh: true,
  },

  pagerConfig: {
    enabled: false
  },

});


const startNodeList = ref([]);
const loading = ref(false);
const loading2 = ref(false);

const dataList = ref({});

const totalObj = ref({});

const dataJson = ref({});//审批发起后 返回的数据
const types = ref("");
const [register, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //打开弹层后的回调
  // resetFields();
  setModalProps({ confirmLoading: false });

  dataList.value = data.selectList;
  types.value = data.types;
  // tableRef.value?.reloadData([])
  // initFn(data.types);
  tableRef.value?.reloadData(data.selectList);
  totalObj.value = checkboxCountFn(data.selectList);
  num.value = data.selectList.length;
  gridOptions.footerData[0].orderNo = "合计：" + num.value + "条";
});

const getTitle = computed(() => ('是否将这些抽成数据进行排除，排除后将不再进行抽成计算！'));


//async function handleSubmit()

const handleSubmit = () => {
  try {

    let data = { list: toRaw(tableRef.value?.getTableData().tableData) };
    num.value = data.list.length;
    //    console.log(data);
    loading.value = true;
    addExcludeZhongBuLog(data).then(res => {
      loading.value = false;
      sunccFn(res)
    })


  } catch (e) {
    console.log(e)
  } finally {
    setModalProps({ confirmLoading: false });

  }
}

function handleDelete(record: Recordable) {
  //console.log(record);
  tableRef.value?.remove(record);
  gridOptions.footerData[0].orderNo = "合计：" + tableRef.value?.getTableData().tableData.length + "条";

}

function sunccFn(result) {
  if (result && result.code == 200) {
    createMessage.success(result.message);
    closeModal()
    emit('success');
  } else {
    createMessage.error(result.message);
  }

}

function cancelFn() {
  //  tableRef.value.clearValidate();
}

function handleOk() {
  visible.value = false;
}


const createActions = (record, rowIndex) => {
  const actions: ActionItem[] = [

    {
      label: '删除',
      color: 'error',
      popConfirm: {
        title: '是否确认删除',
        confirm: () => {
          handleDelete(record)
        },
      },
    },
  ];

  return actions;
};

</script>
<style lang="less">
.item-row {
  margin: 0 auto;

  //padding-left: 5%;
  .ant-form-item-label {
    flex: 0 0 28.5%;
    max-width: 28.5%;
  }
}

.lqfp-8 {
  //padding-left: 9% !important;

}
</style>
